package com.wanpinghui.supplier.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.yunshl.yunshllibrary.utils.TextUtil;
import com.yunshl.yunshllibrary.view.YunShlEditText;

import org.xutils.common.util.DensityUtil;


/***
 * 功能描述:通用类似
 * <p><span>Name</span>&nbsp&nbsp&nbsp<span>Value</span></p> 的item
 * 作者:qiujialiu
 * 时间:2016-08-26
 *
 * <attr name="input_type">
 * <enum name="type_number" value="1" />
 * <enum name="type_number_alphabet" value="8" />
 * <enum name="type_char" value="2" />
 * <enum name="type_number_decimal" value="3" />
 * <enum name="type_email" value="4" />
 * <enum name="type_phone" value="5" />
 * <enum name="type_password" value="6" />
 * </attr>
 ***/
public class NormalNameValueItem extends LinearLayout {
    public CheckBoxChangeListener listener;
    private Context mContext;
    private String hint;
    private String content;
    private boolean haveImage;
    private boolean haveLine;
    private boolean borderTop;
    private boolean borderBottom;
    private boolean haveFocus;
    private String name;
    private View mView;
    private YunShlEditText mEditTextContent;
    private ImageView mImageViewArrows;
    private TextView mTextViewName;
    private LinearLayout mLinearLayoutItem;
    private LinearLayout bg;
    private View mViewBorderBottom;
    private View mViewBorderTop;
    private View mViewLine;
    private int maxLines;
    private int macLength;
    private float heigh;
    private int inputType;
    private int gravity;
    private float lineSpacingExtra;
    private OnFocusChangeListener mFocusChangeListener;

    public NormalNameValueItem(Context context) {
        this(context, null);
    }

    public NormalNameValueItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NormalNameValueItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray mTypedArray = null;
        if (attrs != null) {
            mTypedArray = getResources().obtainAttributes(attrs,
                    R.styleable.NormalNameValueItem);
            hint = mTypedArray.getString(R.styleable.NormalNameValueItem_context_hint);
            content = mTypedArray.getString(R.styleable.NormalNameValueItem_context);
            haveImage = mTypedArray.getBoolean(R.styleable.NormalNameValueItem_have_arrows, false);
            name = mTypedArray.getString(R.styleable.NormalNameValueItem_content_name);
            haveFocus = mTypedArray.getBoolean(R.styleable.NormalNameValueItem_edit_have_focus, false);
            haveLine = mTypedArray.getBoolean(R.styleable.NormalNameValueItem_have_line, false);
            maxLines = mTypedArray.getInt(R.styleable.NormalNameValueItem_max_lines, 0);
            macLength = mTypedArray.getInt(R.styleable.NormalNameValueItem_max_length, 0);
            heigh = mTypedArray.getFloat(R.styleable.NormalNameValueItem_heighs, 0);
            inputType = mTypedArray.getInt(R.styleable.NormalNameValueItem_input_type, 0);
            gravity = mTypedArray.getInt(R.styleable.NormalNameValueItem_text_gravity, 0);
            borderBottom = mTypedArray.getBoolean(R.styleable.NormalNameValueItem_have_bottom_border, true);
            borderTop = mTypedArray.getBoolean(R.styleable.NormalNameValueItem_have_top_border, false);
            lineSpacingExtra = mTypedArray.getFloat(R.styleable.NormalNameValueItem_line_spacing_extra, 0);
        }
        initView();
        if (mTypedArray != null) {
            mTypedArray.recycle();
        }
    }

    public void setListener(CheckBoxChangeListener listener) {
        this.listener = listener;
    }

    public void setLocatonEnd() {
        mEditTextContent.setSelection(mEditTextContent.getText().length());
    }

    public void setEtRight() {
        mEditTextContent.setGravity(Gravity.RIGHT);
    }

    public void setEtColor(int id) {
        mEditTextContent.setTextColor(ContextCompat.getColor(mContext, id));
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        mView = inflater.inflate(R.layout.view_name_value_base, this);
        mEditTextContent = (YunShlEditText) mView.findViewById(R.id.edt_value);

        mLinearLayoutItem = (LinearLayout) mView.findViewById(R.id.ll_item);
        bg = (LinearLayout) mView.findViewById(R.id.ll_bg);
        mTextViewName = (TextView) mView.findViewById(R.id.tv_names);
        mViewBorderBottom = mView.findViewById(R.id.view_border_bottom);
        mViewBorderTop = mView.findViewById(R.id.view_border_top);
        mViewLine = mView.findViewById(R.id.view_line);
        mImageViewArrows = (ImageView) mView.findViewById(R.id.iv_arrows);
        if (TextUtil.isNotEmpty(hint)) {
            mEditTextContent.setHint(hint);
        }
        if (TextUtil.isNotEmpty(content)) {
            mEditTextContent.setText(content);
        }
        if (heigh != 0) {
            ViewGroup.LayoutParams params = mLinearLayoutItem.getLayoutParams();
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            params.height = DensityUtil.dip2px(heigh);
            mLinearLayoutItem.setLayoutParams(params);
        }
        if (TextUtil.isNotEmpty(name)) {
            mTextViewName.setText(name);
        }

        if (haveImage) {
            mImageViewArrows.setVisibility(VISIBLE);
        } else {
            mImageViewArrows.setVisibility(GONE);
        }

        if (lineSpacingExtra > 0f) {
            mEditTextContent.setLineSpacing(lineSpacingExtra, 1f);
        }

        if (gravity == 1) {
            mEditTextContent.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        } else {
            mEditTextContent.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        }

        if (borderBottom) {
            mViewBorderBottom.setVisibility(VISIBLE);
            mViewLine.setVisibility(GONE);
        } else {
            mViewBorderBottom.setVisibility(GONE);
            if (haveLine) {
                mViewLine.setVisibility(VISIBLE);
            } else {
                mViewLine.setVisibility(GONE);
            }
        }

        if (borderTop) {
            mViewBorderTop.setVisibility(VISIBLE);
        } else {
            mViewBorderTop.setVisibility(GONE);
        }

        if (haveFocus) {
            mEditTextContent.setFocusable(true);
            mEditTextContent.setFocusableInTouchMode(true);
            //mEditTextContent.requestFocus();
        } else {
            mEditTextContent.setFocusable(false);
            mEditTextContent.setFocusableInTouchMode(false);
            mEditTextContent.clearFocus();
        }
        mEditTextContent.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (mFocusChangeListener != null) {
                    mFocusChangeListener.onFocusChange(v, hasFocus);
                }
            }
        });
        if (maxLines > 1) {
            mEditTextContent.setMaxLines(maxLines);
            mEditTextContent.setSingleLine(false);
        } else if (maxLines == 1) {
            mEditTextContent.setSingleLine(true);
            mEditTextContent.setEllipsize(TextUtils.TruncateAt.END);
        } else {
            mEditTextContent.setMaxLines(Integer.MAX_VALUE);
            mEditTextContent.setSingleLine(false);
        }
        if (macLength > 0) {
            mEditTextContent.setFilters(new InputFilter[]{new InputFilter.LengthFilter(macLength)});
        }
        switch (inputType) {
            case 1:
                mEditTextContent.setType(YunShlEditText.TYPE_NUMBER);
                mEditTextContent.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
            case 2:
                mEditTextContent.setType(YunShlEditText.CATEGORY_CHARACTER);
                break;
            case 3:
                mEditTextContent.setType(YunShlEditText.TYPE_NUMBER_DECIMAL);
                mEditTextContent.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
                break;
            case 4:
                mEditTextContent.setType(YunShlEditText.CATEGORY_EMAIL);
                break;
            case 5:
                mEditTextContent.setInputType(InputType.TYPE_CLASS_PHONE);
                mEditTextContent.setType(YunShlEditText.NO_FILTER);
                break;
            case 6:
                mEditTextContent.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                break;
            case 8:
                mEditTextContent.setType(YunShlEditText.CATEGORY_NUMBER_);
                break;
            default:
                break;
        }

        mEditTextContent.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (haveFocus) {
                    return false;
                } else {
                    NormalNameValueItem.this.onTouchEvent(event);
                    return true;
                }
            }
        });

    }

    public void setFous(boolean isFous) {
        if (isFous) {
            mEditTextContent.setFocusable(true);
            mEditTextContent.setFocusableInTouchMode(true);
            //mEditTextContent.requestFocus();
        } else {
            mEditTextContent.setFocusable(false);
            mEditTextContent.setFocusableInTouchMode(false);
            mEditTextContent.clearFocus();
        }
    }

    public void setContentFous(boolean isFous) {
        if (isFous) {
            mView.setFocusable(true);
            mView.setFocusableInTouchMode(true);
            //mEditTextContent.requestFocus();
        } else {
            mView.setFocusable(false);
            mView.setFocusableInTouchMode(false);
            mView.clearFocus();
        }
    }

    public YunShlEditText getContentEditText() {
        return mEditTextContent;
    }

    public void setColor(int color) {
        mEditTextContent.setTextColor(color);
    }

    public void setColors(int color) {
        mTextViewName.setTextColor(color);
    }

    public void setSize(float size) {
        mTextViewName.setTextSize(size);
    }

    public void addView(View view) {

    }

    public String getContent() {
        try {
            return mEditTextContent.getText().toString().trim();
        } catch (Exception e) {
            return null;
        }
    }

    public void setContent(CharSequence content) {
        if (TextUtil.isNotEmpty(content)) {
            mEditTextContent.setText(content);
        } else {
            mEditTextContent.setText("");
        }
    }

    public String getName() {
        try {
            return mTextViewName.getText().toString();
        } catch (Exception e) {
            return null;
        }
    }

    public void setName(CharSequence name) {
        if (TextUtil.isNotEmpty(name)) {
            mTextViewName.setText(name);
        }
    }

    public void setContentHint(CharSequence hint) {
        if (TextUtil.isNotEmpty(hint)) {
            mEditTextContent.setHint(hint);
        }
    }

    public void setmEditTextHintColor(int color) {
        mEditTextContent.setHintTextColor(color);
    }

//    public void setEditTextContentColor(){
//        mEditTextContent.setTextColor(mContext.getResources().getColor(R.color.color_bar));
//    }

    public void hideImage() {
        mImageViewArrows.setVisibility(GONE);
    }

    public void showImage() {
        mImageViewArrows.setVisibility(VISIBLE);
    }

    public void showLine() {
        mViewLine.setVisibility(VISIBLE);
    }

    public void hideLine() {
        mViewLine.setVisibility(GONE);
    }

    public void showBottomBorder() {
        mViewBorderBottom.setVisibility(VISIBLE);
    }

    public void hideBottomBorder() {
        mViewBorderBottom.setVisibility(GONE);
    }

    public void showTopBorder() {
        mViewBorderTop.setVisibility(VISIBLE);
    }

    public void hideTopBorder() {
        mViewBorderTop.setVisibility(GONE);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
//        mLinearLayoutItem.setOnClickListener(l);
        mEditTextContent.setOnClickListener(l);
        super.setOnClickListener(l);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        if (enabled) {
            showImage();
        } else {
            hideImage();
        }
    }

    public void setEditTextFocusChangeListener(OnFocusChangeListener listener) {
        mFocusChangeListener = listener;
    }

    public interface CheckBoxChangeListener {
        void CheckBoxListener(boolean isCheck);
    }
}
